<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>超市订单管理系统</title>
    <link type="text/css" rel="stylesheet" href="../../static/css/style.css" th:href="@{/css/style.css}"/>
    <link type="text/css" rel="stylesheet" href="../../static/css/public.css" th:href="@{/css/public.css}"/>
    <link type="text/css" rel="stylesheet" href="../../static/css/pagination.css" th:href="@{/css/pagination.css}"/>
</head>
<body>

<!--头部-->
<div th:replace="commons/common::header"></div>
<!--时间-->
<div th:replace="commons/common::time"></div>
<!--主体内容-->
<section class="publicMian ">
    <!--left-->
    <div th:replace="commons/common::left"></div>
    <div class="right">
        <div class="location">
            <strong>你现在所在的位置是:</strong>
            <span>用户管理页面</span>
        </div>
        <div class="search">
            <form>
                <span>用户名：</span>
                <input name="userName" id="userName" class="input-text" type="text">

                <span>用户角色：</span>
                <select name="userRole" id="userRole">
                    <option value="">请选择</option>
                </select>
                <input value="查 询" type="button" id="searchBtn">
                <a href="add.html" th:href="@{/user/add}">添加用户</a>
            </form>
        </div>
        <!--用户-->
        <table class="providerTable" cellpadding="0" cellspacing="0">
            <tr class="firstTr">
                <th width="10%">用户编码</th>
                <th width="20%">用户名称</th>
                <th width="10%">性别</th>
                <th width="10%">出生日期</th>
                <th width="10%">电话</th>
                <th width="10%">用户角色</th>
                <th width="30%">操作</th>
            </tr>
            <tbody id="tbody">

            </tbody>
        </table>
        <div id="Pagination">

        </div>
    </div>
</section>



<div th:replace="commons/common::footer"></div>
<script type="text/javascript" src="../../static/js/time.js" th:src="@{/js/time.js}"></script>
<script type="text/javascript" src="../../static/js/jquery-1.8.3.min.js" th:src="@{/js/jquery-1.8.3.min.js}"></script>
<script type="text/javascript" src="../../static/js/common.js" th:src="@{/js/common.js}"></script>
<script type="text/javascript" src="../../static/calendar/WdatePicker.js" th:src="@{/calendar/WdatePicker.js}"></script>
<script type="text/javascript" src="../../static/js/rollpage.js" th:src="@{/js/rollpage.js}"></script>
<script type="text/javascript" src="../../static/js/jquery.pagination.js" th:src="@{/js/jquery.pagination.js}"></script>
<script>
    $(function () {
       queryPage(0);
    });
    const jsonObj={
        pageSize:5
    };
    function queryPage(pageIndex) {
        jsonObj.pageIndex=pageIndex+1;
        $.ajax({
            url:"[[@{/user/dolist}]]",
            type:"post",
            data:jsonObj,
            beforeSend:function () {
                return true;
            },
            success:function (result) {
                if (result.success) {
                    const pager=result.page;
                    const data=pager.list;
                    let content='';
                    const rolelist=result.param.rolelist;
                    let roleSelect='<option value="">请选择</option>';
                    $.each(rolelist,function (i,role) {
                        if (role.id==result.param.userRole){
                            roleSelect+='<option value="'+role.id+'" selected>'+role.roleName+'</option>'
                        }else{
                            roleSelect+='<option value="'+role.id+'" >'+role.roleName+'</option>'
                        }

                    });
                    $("#userRole").html(roleSelect);
                    $.each(data,function (i,user) {
                        const gender = user.gender == 1 ? '男' : '女';
                        content+='<tr>';
                        content+='	<td>';
                        content+='		<span>'+user.userCode+'</span>';
                        content+='	</td>';
                        content+='	<td>';
                        content+='		<span>'+user.userName+'</span>';
                        content+='	</td>';
                        content+='	<td>';
                        content+='		<span>'+gender+'</span>';
                        content+='	</td>';
                        content+='	<td>';
                        content+='		<span>'+user.birthday+'</span>';
                        content+='	</td>';
                        content+='	<td>';
                        content+='		<span>'+user.phone+'</span>';
                        content+='	</td>';
                        content+='	<td>';
                        content+='		<span>'+user.userRoleName+'</span>';
                        content+='	</td>';
                        content+='	<td>';
                        content+='		<span>';
                        content+='			<a class="viewUser"  href="[[@{/user/view?id=}]]'+user.id+'"><img  src="[[@{/images/read.png}]]" alt="查看" title="查看"/></a>';
                        content+='		</span>';
                        content+='		<span>';
                        content+='			<a class="modifyUser" href="[[@{/user/modify?id=}]]'+user.id+'"><img  src="[[@{/images/xiugai.png}]]" alt="修改" title="修改"/></a>';
                        content+='		</span>';
                        content+='		<span>';
                        content+='			<a  class="deleteUser" onclick="deleteUser('+user.id+')" id="deleteUser"}><img  src="[[@{/images/schu.png}]]" alt="删除" title="删除"/></a>';
                        content+='		</span>';
                        content+='	</td>';
                        content+='</tr>';
                    });
                    $("#tbody").html(content);
                    $("#Pagination").pagination(pager.totalCount, {
                        num_edge_entries: 2,
                        num_display_entries: 4,
                        callback: queryPage,
                        items_per_page:pager.pageSize,
                        current_page:(pager.pageIndex-1),
                        prev_text:"上一页",
                        next_text:"下一页"
                    });
                }
            }
        })
    }
    $("#searchBtn").click(function () {
        var userName=$("#userName").val();
        var userRole=$("#userRole").val();
        jsonObj.userName=userName;
        jsonObj.userRole=userRole;
        queryPage(0);
    })
    function deleteUser(userId) {
        $.ajax({
            url:"[[@{/user/delete}]]",
            type:"post",
            data:{
                id:userId
            },
            success:function (result) {
                if (result.success) {
                    window.location.href="[[@{/user/list}]]"
                }
            }
        })
    }
</script>

</body>
</html>